//
//  BAAlbumListVC.m
//  BabyAlbum
//
//  Created by master on 17/2/14.
//  Copyright © 2017年 master. All rights reserved.
//

#import "BAAlbumListVC.h"
#import "BAAlbumListLayout.h"
#import "BAAlbumCellNode.h"
#import "BAAlbumDetailVC.h"
#import "BAResource.h"
@interface BAAlbumListVC () <ASCollectionDataSource, ASCollectionDelegateFlowLayout, BACollectionViewLayoutDelegate,ASCollectionViewLayoutInspecting,BAAlbumDataProtocol>
//@property (nonatomic, strong) BAAlbumIndexManager *albumDataManager;

@end
static NSString *cellIndentifier = @"BAAlbumCell";

@implementation BAAlbumListVC



- (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize {
    return [ASInsetLayoutSpec insetLayoutSpecWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) child:self.collectionNode];
}

- (void)didLoad {
    [super didLoad];
    [self viewInit];
    
//    [self viewInit];
    [BAALBUM_DATA setDelegate:self];

    // Do any additional setup after loading the view.
}

- (void)viewInit
{
    BAAlbumListLayout *layout = [[BAAlbumListLayout alloc] init];
    layout.numberOfColumns = 2;
//    layout.headerHeight = 10;
//    layout.sectionInset = UIEdgeInsetsMake(44, 0, 0, 0);
    self.collectionNode = [[ASCollectionNode alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) collectionViewLayout:layout];
    self.collectionNode.dataSource = self;
    self.collectionNode.delegate = self;
    self.collectionNode.view.showsHorizontalScrollIndicator = NO;
    self.collectionNode.view.showsVerticalScrollIndicator = NO;
    
    self.collectionNode.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.collectionNode.backgroundColor = RGB_255(235, 240, 239);
    
    self.collectionNode.view.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [_collectionNode registerSupplementaryNodeOfKind:UICollectionElementKindSectionHeader];
    
    //    [self.collectionNode.view registerNib:[UINib nibWithNibName:@"BAAlbumCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:cellIndentifier];

//    if (self = [super initWithNode:[[ASDisplayNode alloc] init]]) {
    [self addSubnode:self.collectionNode];
//        [self viewInit];
//    }
    _collectionNode.view.layoutInspector = self;
}

#pragma mark - ASCollectionView Data Source

- (ASCellNodeBlock)collectionNode:(ASCollectionNode *)collectionNode nodeBlockForItemAtIndexPath:(NSIndexPath *)indexPath;
{
    BAAlbumModel *model = BAALBUM_DATA.albumList[indexPath.row];
    return ^{
//        UIImage *image = [model.cover coverImage];
        return [[BAAlbumCellNode alloc] initWithModel:model];
    };
}


- (NSInteger)collectionNode:(ASCollectionNode *)collectionNode numberOfItemsInSection:(NSInteger)section
{
    return BAALBUM_DATA.albumList.count;
}

- (NSInteger)numberOfSectionsInCollectionNode:(ASCollectionNode *)collectionNode
{
    return 1;
}



- (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForNodeAtIndexPath:(NSIndexPath *)indexPath
{
    BAAlbumListLayout *layout = (BAAlbumListLayout *)[collectionView collectionViewLayout];
//    NSLog(@"%@",NSStringFromCGSize([layout itemSizeAtIndexPath:indexPath]));
    return ASSizeRangeMake(CGSizeZero, [layout itemSizeAtIndexPath:indexPath]);
}

- (ASSizeRange)collectionView:(ASCollectionView *)collectionView constrainedSizeForSupplementaryNodeOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    BAAlbumListLayout *layout = (BAAlbumListLayout *)[collectionView collectionViewLayout];
    return ASSizeRangeMake(CGSizeZero, [layout headerSizeForSection:indexPath.section]);
}

- (ASScrollDirection)scrollableDirections
{
    return ASScrollDirectionVerticalDirections;
}

/**
 * Asks the inspector for the number of supplementary views for the given kind in the specified section.
 */
- (NSUInteger)collectionView:(ASCollectionView *)collectionView supplementaryNodesOfKind:(NSString *)kind inSection:(NSUInteger)section
{
    return 0;
}

- (ASCellNode *)collectionNode:(ASCollectionNode *)collectionNode nodeForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
//    NSDictionary *textAttributes = @{
//                                     NSFontAttributeName: [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline],
//                                     NSForegroundColorAttributeName: [UIColor grayColor]
//                                     };
//    UIEdgeInsets textInsets = UIEdgeInsetsMake(11.0, 0, 11.0, 0);
//    ASTextCellNode *textCellNode = [[ASTextCellNode alloc] initWithAttributes:textAttributes insets:textInsets];
//    textCellNode.text = [NSString stringWithFormat:@"Section %zd", indexPath.section + 1];
    return [[ASCellNode alloc] init];
}



- (CGSize)collectionView:(ASCollectionNode *)collectionNode layout:(UICollectionViewLayout *)collectionViewLayout originalItemSizeAtIndexPath:(NSIndexPath *)indexPath
{
    ASCellNode *cellNode = [collectionNode nodeForItemAtIndexPath:indexPath];
    if ([cellNode isKindOfClass:[BAAlbumCellNode class]]) {
        CGSize size =  [(BAAlbumCellNode *)cellNode calculatedSize];
//        NSLog(@"%@",NSStringFromCGSize(size));
        return size;
    }
    return CGSizeMake(150, 2400);
//    else {
//        return CGSizeMake(150, 240);  // In kShowUICollectionViewCells = YES mode, make those cells 100x100.
//    }
}

- (void)collectionNode:(ASCollectionNode *)collectionNode didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    [BAALBUM_DATA removeDelegate:self];
    BAALBUM_DATA.currentIndex = indexPath.row;
    [BAALBUM_DATA setDelegate:self];
    BAAlbumDetailVC *detail = [[BAAlbumDetailVC alloc] init];
    [_manager.navigationController pushViewController:detail animated:YES];
}

//- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//    return [_collectionNode.view dequeueReusableCellWithReuseIdentifier:kReuseIdentifier forIndexPath:indexPath];
//}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    return nil;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSArray<ASCellNode *> *array = [self.collectionNode visibleNodes];
    [BAALBUM_DATA removeDelegate:self];
    BAALBUM_DATA.currentIndex = [array lastObject].indexPath.row;
    [BAALBUM_DATA setDelegate:self];
}


- (void)albumManager:(BAAlbumIndexManager *)manager didChangeToIndex:(NSInteger)index {
    if([self.collectionNode numberOfItemsInSection:0] > BAALBUM_DATA.currentIndex && self.collectionNode.view.numberOfSections > 0) {
        [self.collectionNode scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UICollectionViewScrollPositionTop animated:NO];
        NSLog(@"List Cover 变更Index:%ld",index);

    }
}
- (void)albumManagerDataSourceDidChanged:(BAAlbumIndexManager *)manager {

    [self.collectionNode reloadData];
    NSLog(@"List Cover 已刷新");

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
